﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using PPI.Client.Logic.Delegate;
using PPI.Client.Logic.Inspection;
using PPI.Client.Utils;
using PPI.Entities;
using PPI.Entities.Inspections;

namespace PPI.Client.Presenter
{
    public interface ISplashInspection
    {
        string ActionMessage { set; }
        Int32 ActionProgress { set; }
        Int32 InspectionId { get; }
        EInspection Inspection { set; }
        void Finish();
    }


    public class SplashInspectionPresenter
    {
        private ISplashInspection m_View;
        private DownloadFile m_Downloader;
        private UpdateBar m_DelegateProgressBar;
        private EInspection m_Inspection;
        private Stack<EInspectionImage> m_ImagesUrlToDownload;

        public SplashInspectionPresenter(ISplashInspection view)
        {
            m_View = view;
            Initialize();
        }

        private void Initialize()
        {
            try
            {
                m_DelegateProgressBar = new UpdateBar(UpdateProgressBar);
                m_Downloader = new DownloadFile(m_DelegateProgressBar);
                m_Downloader.FinishDownloadFile += new EventHandler(m_Downloader_FinishDownloadFile);
                DownloadInspection();
                if (m_Inspection != null)
                {
                    PrepareImagesToDownload();
                    DownloadInspectionImages();

                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        void m_Downloader_FinishDownloadFile(object sender, EventArgs e)
        {
            try
            {
                DownloadInspectionImages();
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void UpdateProgressBar(int percentage)
        {
            m_View.ActionProgress = percentage;
        }

        private void DownloadInspection()
        {
            m_View.ActionMessage = "Downloading Data ...";
            m_Inspection = LInspectionResult.GetInspectionResultByInspection(m_View.InspectionId).Inspection; 
            //LInspectionResult.GetInspectionResult(m_View.InspectionId).Inspection;

        }

        private void DownloadInspectionImages()
        {
            m_View.ActionMessage = "Downloading Images ...";
            if(m_ImagesUrlToDownload.Count > 0)
            {
                EInspectionImage tmpImage = m_ImagesUrlToDownload.Pop();
                m_Downloader.SetUrl(tmpImage.GetImageUrl(tmpImage.Inspection.Id));
                m_Downloader.SetFileDestination(tmpImage.GetTemporaryFolder());
                if (!File.Exists(tmpImage.GetTemporaryFolder()))
                    m_Downloader.StartDownload();
                else
                    DownloadInspectionImages();
            }else
            {
                m_View.ActionMessage = "Finish Downloading images ...";

                foreach (EInspectionImage i in m_Inspection.Images)
                {
                    if (File.Exists(i.GetTemporaryFolder()))
                        i.ImageFile = new FileInfo(i.GetTemporaryFolder());
                }
                m_View.Inspection = m_Inspection;
                m_View.Finish();
            }
        }

        private void PrepareImagesToDownload()
        {
            m_View.ActionMessage = "Preparing Images ...";
            m_ImagesUrlToDownload = new Stack<EInspectionImage>(m_Inspection.Images);
        }
    }
}
